package web;

import com.alibaba.fastjson2.JSONObject;
import core.AjaxResult;
import core.BaseHttpServlet;
import util.RequestUtils;
import util.ResponseUtils;
import dao.impl.StudentDaoImpl;
import domain.Student;
import service.StudentService;
import service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;


/**
 * @author 于金鹏
 * @Date 2024/12/16 16:29
 * @Package StudentCheckSystem
 */
@WebServlet("/student/*")
public class StudentServlet extends BaseHttpServlet {
    private StudentService studentService;

    @Override
    public void init() throws ServletException {
        studentService = new StudentServiceImpl(new StudentDaoImpl());
    }

    // 按id查询
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (!req.getParameterMap().containsKey("id")) {
            ResponseUtils.responseJSON(resp, AjaxResult.error(400, "请传入id"));
            return;
        }
        int id = Integer.parseInt(req.getParameter("id"));
        executeBusiness(resp, response -> {
            Student student = studentService.getStudentById(id);
            ResponseUtils.responseJSON(resp, AjaxResult.success(student));
        });
    }

    // 查询所有
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pathInfo = req.getPathInfo();
        if (pathInfo == null) {
            Student student = RequestUtils.getReqJsonBody(req, Student.class);
            executeBusiness(resp, response -> {
                ResponseUtils.responseJSON(resp, AjaxResult.success(studentService.addStudent(student)));
            });
        } else if (pathInfo.equals("/list")) {
            Student student = RequestUtils.getReqJsonBody(req, Student.class);
            int pageNum = (req.getParameter("pageNum") == null || req.getParameter("pageNum") == "") ? 1 : Integer.parseInt(req.getParameter("pageNum"));
            int pageSize = (req.getParameter("pageSize") == null || req.getParameter("pageSize") == "") ? 10 : Integer.parseInt(req.getParameter("pageSize"));
            executeBusiness(resp, response -> {
                ResponseUtils.responseJSON(resp, AjaxResult.success(studentService.getStudentPage(pageNum, pageSize, student)));
            });
        }
    }

    // 删除
    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (!req.getParameterMap().containsKey("id")) {
            ResponseUtils.responseJSON(resp, AjaxResult.error(400, "参数不能为空"));
            return;
        }
        int id = Integer.parseInt(req.getParameter("id"));
        executeBusiness(resp, response -> {
            ResponseUtils.responseJSON(resp, AjaxResult.success(studentService.deleteStudent(id)));
        });
    }

    // 修改
    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Student student = RequestUtils.getReqJsonBody(req, Student.class);
        executeBusiness(resp, response -> {
            ResponseUtils.responseJSON(resp, AjaxResult.success(studentService.updateStudent(student)));
        });
    }
}
